package com.vcar.LDiceClient.module.hallModule
{
	import caurina.transitions.Tweener;
	
	import com.vcar.LDiceClient.config.GlobalEventName;
	import com.vcar.LDiceClient.config.MediatorName;
	import com.vcar.LDiceClient.config.ModuleName;
	import com.vcar.LDiceClient.config.ProxyNames;
	import com.vcar.LDiceClient.dataproxy.RequestMessage;
	import com.vcar.LDiceClient.dataproxy.ResponseMessage;
	import com.vcar.LDiceClient.dataproxy.SocketProxy;
	import com.vcar.LDiceClient.dataproxy.module.ModuleInfo;
	import com.vcar.LDiceClient.dataproxy.user.UserProxy;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.controls.Button;
	import mx.events.ListEvent;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	public class hallModuleMediator extends Mediator
	{
		private var storeList:Array=new Array();
		public function hallModuleMediator(viewComponent:Object=null)
		{
			super(MediatorName.hallModuleMediator, viewComponent);
			getStoreList();
			
			view.joinStoreBtn.enabled=false;
			view.joinStoreBtn.addEventListener(MouseEvent.CLICK, joinStoreHandle);
			view.getStoreBtn.addEventListener(MouseEvent.CLICK, getStoreList);
			view.shopList.addEventListener(ListEvent.ITEM_CLICK,shopListItemHandle);
		}
		

		public function get view():hallModule
		{
			return viewComponent as hallModule;
		}
		
		override public function onRemove() : void
		{
			view.joinStoreBtn.removeEventListener(MouseEvent.CLICK, joinStoreHandle);
			view.getStoreBtn.removeEventListener(MouseEvent.CLICK, getStoreList);
			view.shopList.removeEventListener(ListEvent.ITEM_CLICK,shopListItemHandle);
		}
		
		override public function listNotificationInterests():Array
		{
			return [GlobalEventName.STORE_JOIN_SUSS,
					GlobalEventName.STORE_GETLIST_SUSS,
					GlobalEventName.STORE_EXIT_SUSS
					];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			var obj:ResponseMessage=notification.getBody() as ResponseMessage;
			switch (notification.getName())
			{
				case GlobalEventName.STORE_JOIN_SUSS://进入成功，加载店铺moduleaa
				{
					var p:UserProxy=facade.retrieveProxy(ProxyNames.UserProxy) as UserProxy;
					if(obj.target==p.currentUserVO.username)
					{
						var m:ModuleInfo=new ModuleInfo(ModuleName.STORE_MODULE);
						sendNotification(GlobalEventName.MODULE_CHANGE,m);
					}
					else
					{
						storeList[obj.object.storeId as int]["countPlayer"]=obj.object.countPlayer.toString();
						view.shopList.dataProvider=storeList;
					}
					
				}
					break;
				case GlobalEventName.STORE_GETLIST_SUSS://获取店铺列表
				{
					showStoreList(obj.object.storeList as ArrayCollection);
				}
					break;
				case GlobalEventName.STORE_EXIT_SUSS://获取店铺列表
				{
					storeList[obj.object.storeId as int]["countPlayer"]=obj.object.countPlayer.toString();
					view.shopList.dataProvider=storeList;
				}
					break;
				
				
			}
		}
		
		/**
		 * 获取店铺列表 
		 * 
		 */		
		private function getStoreList(e:Event=null):void
		{
			var reMessage:RequestMessage = new RequestMessage();
			var proxy:SocketProxy = facade.retrieveProxy(ProxyNames.SocketProxy) as SocketProxy;
			reMessage.type=5;
			reMessage.subtype=2;
			proxy.send(reMessage);		
			//view.chooseLabel.text="列表读取中...";
		}
		
		/**
		 * 处理店铺列表 
		 * 
		 */		
		private function showStoreList(_storeList:ArrayCollection):void
		{
			storeList=new Array();
			for(var i:int=0;i<_storeList.length;i++)
			{
				storeList.push({
					"label":_storeList[i]["storeName"],//"("+_storeList[i]["countPlayer"]+")",
					"countPlayer":_storeList[i]["countPlayer"],
					"maxPlayer":_storeList[i]["maxPlayer"],
					"storeId":int(_storeList[i]["storeId"])
				});
			}
			storeList.sortOn("storeId");
			view.shopList.dataProvider=storeList;
			storeShow();
		}
		
		
		
		/**
		 * 进入商铺
		 */		
		private function joinStoreHandle(e:MouseEvent):void
		{
			
			
			if(view.shopList.selectedItem != null || e.currentTarget.name<storeList.length)
			{
				var reMessage:RequestMessage = new RequestMessage();
				var proxy:SocketProxy = facade.retrieveProxy(ProxyNames.SocketProxy) as SocketProxy;
				reMessage.type=6; 
				reMessage.subtype=3;
				if (view.shopList.selectedItem != null)
				{
					reMessage.params.storeId=view.shopList.selectedItem.storeId;					
				}
				else
				{
					reMessage.params.storeId=e.currentTarget.name;					
				}
				proxy.send(reMessage);
			}
			else
			{
				Alert.show("请选择进入店铺");
			}
		}
		
		
		private function shopListItemHandle(e:ListEvent):void
		{
			if(view.shopList.selectedItem.storeId > 0)
			{
				view.joinStoreBtn.enabled=true;
			}
		}
		private var playerPic:Array=new Array;
		private var changeI:int=0;
		private var com:Boolean=true;
		private function storeShow():void
		{
			view.textCav.verticalScrollPolicy="off";
			view.textCav.horizontalScrollPolicy="off";
			var storeBgPic:storeBg=new storeBg();
			storeBgPic.x=0.5*view.storePicShow.width;
			storeBgPic.y=0.5*view.storePicShow.height;
			view.storePicShow.addChild(storeBgPic);
			storeBgPic.scaleX=storeBgPic.scaleY=0.8;
			view.testC.visible=false;
			for (var i:int=0;i<7;i++)
			{				
				var testPic:hallPic=new hallPic();
				var radius:Number=5000;				
				view.storePicShow.addChild(testPic);
				testPic.x=(1-Math.cos(Math.PI/180*(6*i+72)))*radius-(radius-0.5*view.storePicShow.width);				
				testPic.y=(0.6*view.storePicShow.height+radius)-Math.sin(Math.PI/180*(6*i+72))*radius;	
				testPic.rotation=(i-3)*3;
				testPic.name=""+i;	
				if(i==6)
				{
					testPic.y=(0.6*view.storePicShow.height+radius)-Math.sin(Math.PI/180*(8*i+66))*radius+10000;		
				}
				
				playerPic.push({
					"x":testPic.x,
					"y":testPic.y,
					"rotation":testPic.rotation
				});
				
				testPic.scaleX=testPic.scaleY=0.8;				
				testPic.addEventListener(MouseEvent.CLICK,joinStoreHandle);
				var testText:storeSign=new storeSign();
				testPic.addChild(testText);			
				testText.visible=true;
				testText.x=0;
				testText.y=-110;
				if (storeList.length>i)	
				{
					testText.storeName.text=storeList[i]["label"];
					testText.playNum.text=storeList[i]["countPlayer"]+"/"+storeList[i]["maxPlayer"];					
				}
				else
				{
					testText.storeName.text="null";
					testText.playNum.text="null";
				}
			}
			
			var btn:Button=new Button();
			var btnT:Button=new Button();
			btnT.width=btn.width=60;
			btnT.height=btn.height=30;
			btnT.x=btn.x=50;
			btn.y=500;
			btnT.y=600;
			btnT.label="返滚";			
			btn.label="滚";
			btn.name="testBtn";
			btnT.name="testBtnT";
			view.storePicShow.addChild(btn);
			view.storePicShow.addChild(btnT);
			btn.addEventListener(MouseEvent.CLICK,testChange);
			btnT.addEventListener(MouseEvent.CLICK,testChangeT);
			
		}
		private function comChange():void
		{
			view.storePicShow.getChildByName("testBtn").addEventListener(MouseEvent.CLICK,testChange);
			view.storePicShow.getChildByName("testBtnT").addEventListener(MouseEvent.CLICK,testChangeT);
		}
		
		private function testChangeT(e:Event=null):void
		{
			if(changeI==7)
			{
				changeI=0;
			}
			
			changeI+=1;	
			Change(changeI);
		}
		private function testChange(e:Event=null):void
		{
			if(changeI==0)
			{
				changeI=7;
			}
			
			changeI+=-1;	
			Change(changeI);
		}
		
		private function Change(changeI:int):void
		{	
			
			
			for(var u:int=0;u<7;u++)
			{	
				var cI:int=u+changeI;
				var aName:String=""+u;
				var changeTager:hallPic=(view.storePicShow.getChildByName(aName) as hallPic);
				if (cI>6)
				{
					cI=cI-7;
				}
				
				var _x:Number=playerPic[cI]["x"];
				var _y:Number=playerPic[cI]["y"];
				var _rotation:Number=playerPic[cI]["rotation"];					
				Tweener.addTween(changeTager,{rotation:_rotation,x:_x,y:_y,time:0.5, transition:"easeOutCubic",onComplete:comChange});	
				
				
				
				
			} 
			view.storePicShow.getChildByName("testBtn").removeEventListener(MouseEvent.CLICK,testChange);	
			view.storePicShow.getChildByName("testBtnT").removeEventListener(MouseEvent.CLICK,testChangeT);
		}
		
		
		

	}
}